import { search_song_list,search_special_list,search_album_list,search_mv_list } from "@/api";

const state={
  search_song_list:{},
  search_special_list:{},
  search_album_list:{},
  search_mv_list:{},
};
const getters={

};
const mutations={
  SEARCH_SONG(state,data){
    for(var i in data.info){

      data.info[i].filename=data.info[i].filename.replace("<em>","")
      data.info[i].filename=data.info[i].filename.replace("</em>","")
    }
    state.search_song_list=data
  },
  SEARCH_SPECIAL(state,data){
    for(var i in data.info){

      data.info[i].imgurl=data.info[i].imgurl.replace("{size}","200")

      data.info[i].specialname=data.info[i].specialname.replace("<em>","")
      data.info[i].specialname=data.info[i].specialname.replace("</em>","")
    }
    state.search_special_list=data
  },
  SEARCH_ALBUM(state,data){
    for(var i in data.info){
      data.info[i].imgurl=data.info[i].imgurl.replace("/{size}/","/")

      data.info[i].albumname=data.info[i].albumname.replace("<em>","")
      data.info[i].albumname=data.info[i].albumname.replace("</em>","")

      data.info[i].publishtime=data.info[i].publishtime.slice(0,-8)
    }
    state.search_album_list=data
  },
  SEARCH_MV(state,data){
    for(var i in data.info){
      data.info[i].imgurl=data.info[i].imgurl.replace("/{size}/","/")

      data.info[i].filename=data.info[i].filename.replace("<em>","")
      data.info[i].filename=data.info[i].filename.replace("</em>","")

      
    }
    state.search_mv_list=data
  }
};
const actions={
  //搜索歌曲
  async search_song({commit},keyword){
    let result=await search_song_list(keyword)
    let a=JSON.parse(result.slice(23,-21))
    if(a.status==1){
      commit("SEARCH_SONG",a.data)
    }else{
      return Promise.reject(new Error("faile"))
    }
  },
  //搜索歌单
  async search_special({commit},keyword){
    let result=await search_special_list(keyword)
    let a=JSON.parse(result.slice(23,-21))
    if(a.status==1){
      commit("SEARCH_SPECIAL",a.data)
    }else{
      return Promise.reject(new Error("faile"))
    }
  },
  //搜索专辑
  async search_album({commit},keyword){
    let result=await search_album_list(keyword)
    if(result.status==1){
      commit("SEARCH_ALBUM",result.data)
    }else{
      return Promise.reject(new Error("faile"))
    }
  },
  //搜索mv
  async search_mv({commit},keyword){
    let result=await search_mv_list(keyword)
    let a=JSON.parse(result.slice(23,-21))
    console.log(a)
    if(a.status==1){
      commit("SEARCH_MV",a.data)
    }else{
      return Promise.reject(new Error("faile"))
    }
  },
};
const modules={

};

export default{
    state,
    getters,
    mutations,
    actions,
    modules
  }